package com.card.client.view;

import com.card.client.dialog.DeleteDialog;
import com.card.client.dialog.EditCreditCardDialog;
import com.card.client.entry.CreditCard;
import com.card.client.entry.Util;
import com.card.client.entry.ViewObject;
import com.card.client.service.CreditCardService;
import com.card.client.service.CreditCardServiceAsync;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.Cell;

public class CreditCardView extends Composite implements ViewObject {

	private static CreditCardViesUiBinder uiBinder = GWT
			.create(CreditCardViesUiBinder.class);

	interface CreditCardViesUiBinder extends UiBinder<Widget, CreditCardView> {
	}

	private final CreditCardServiceAsync ccs = GWT.create(CreditCardService.class);
	
	private Grid grid = getGrid();
	
	@UiField HorizontalPanel table;
	
	@UiField Button addCreditCard;
	
	@UiField Label descript;
	
	@UiField HTML sum;
	
	private int total;
	
	private String[] title = CreditCard.title;

	public CreditCardView() {
		initWidget(uiBinder.createAndBindUi(this));
		table.setWidth("100%");
		table.add(grid);
		grid.setWidth("100%");
		descript.setStyleName("descript");
		TableAction action = new TableAction();
		action.setCreditCardView(this);
		grid.addClickHandler(action);
		sum.setWidth("100%");
		sum.setStyleName("sum");
	}

	public void onLoad(){
		ccs.getAllCard(new AsyncCallback<CreditCard[]>() {
			public void onSuccess(CreditCard[] result) {
				if(result!=null){
					total = 0;
					setData(result);
					sum.setText("信用额度共计：" + Util.intToStr(total) + "元");
				}
			}
			public void onFailure(Throwable caught) {}
		});
	}
	
	private void setData(CreditCard[] result){
		if(result.length==0){
			grid.resize(1, title.length);
		} else {
			grid.resize(result.length + 1, title.length);
		}
		for(int i=0;i<title.length;i++){
			if(i==0){
				grid.getColumnFormatter().setWidth(i, "10%");
			} else if (i == 1){
				grid.getColumnFormatter().setWidth(i, "30%");
			} else {
				grid.getColumnFormatter().setWidth(i, "20%");
			}
		}
		
		
		for(int i=0;i<title.length;i++){
			grid.setText(0, i, title[i]);
			if(i!=(title.length-1)){
				grid.getCellFormatter().addStyleName(0, i, "header");
			} else {
				grid.getCellFormatter().addStyleName(0, i, "header-n");
			}
		}
		for(int i=0;i<result.length;i++){
			int a = 0;
			CreditCard cc = result[i];
			grid.setText(i+1, a++, Long.toString(cc.getId()));
			grid.setText(i+1, a++, cc.getName());
			grid.setText(i+1, a++, cc.getItem());
			grid.setText(i+1, a++, Util.intToStr(cc.getAmount()) + "元");
			total = total + cc.getAmount();
			grid.setText(i+1, a++, "每月" + cc.getBillDate() + "日");
			reload(i+1);
		}
	}
	
	public Grid getGrid() {
		if (grid != null){
			return grid;
		}
		return new GreenMouseEventGrid();
	}
	
	@UiHandler("addCreditCard")
	void onClick(ClickEvent e) {
		EditCreditCardDialog acd = new EditCreditCardDialog("添加卡片", "添加一张卡片信息", this, null, Util.DIALOG_CONTROL_ADD);
		acd.center();
		acd.show();
	}
	
	private void reload(int row) {
		descript.setText(null);
		if(row%2!=0){
			for(int i=0;i<title.length;i++){
				if(i==title.length-1){
					grid.getCellFormatter().setStyleName(row, i, "row0-n");
				} else {
					grid.getCellFormatter().setStyleName(row, i, "row0");
				}
			}
		} else {
			for(int i=0;i<title.length;i++){
				if(i==title.length-1){
					grid.getCellFormatter().setStyleName(row, i, "row1-n");
				} else {
					grid.getCellFormatter().setStyleName(row, i, "row1");
				}
			}
		}
	}
	
	class GreenMouseEventGrid extends MouseEventGrid {

		public void onMouseOut(Element td, int row) {
			reload(row);
		}

		public void onMouseOver(Element td, int row) {
			Element tr = DOM.getParent(td);
			int column = DOM.getChildIndex(tr, td);
			String name = grid.getHTML(row, 1);
			if(column == 0){
				descript.setText("删除卡片 “" + name + "” ");
			} else {
				descript.setText("修改卡片 “" + name + "” 信息");
			}
			for(int i=0;i<title.length;i++){
				if(i==title.length-1){
					grid.getCellFormatter().setStyleName(row, i, "rows-n");
				} else {
					grid.getCellFormatter().setStyleName(row, i, "rows");
				}
			}
		}
    }
	
	public class TableAction implements ClickHandler {
		
		private CreditCardView creditCardView;
		
		public void onClick(ClickEvent event) {
			HTMLTable table = (HTMLTable)event.getSource();
			Cell Mycell = table.getCellForEvent(event);
			if( Mycell == null ) return;
			int row = Mycell.getRowIndex();
			int col = Mycell.getCellIndex();
			reload(row);
			CreditCard cc = getObject(row);
			Long id = Long.valueOf(grid.getHTML(row, 0));
			cc.setId(id);
			if(col == 0){
				String des = "确认删除卡片 “" + cc.getName() + "” 么？";
				DeleteDialog dd = new DeleteDialog("卡片", des, creditCardView, id);
				dd.show();
			} else {
				String des = "修改卡片 “" + cc.getName() + "” 的信息";
				EditCreditCardDialog ecd = new EditCreditCardDialog("修改卡片信息", des, creditCardView, cc, Util.DIALOG_CONTROL_EDIT);
				ecd.show();
			}
		}

		public void setCreditCardView(CreditCardView creditCardView){
			this.creditCardView = creditCardView;
		}
	}
	
	public void delDate(Long id) {
		if(id == null){
			return;
		}
		ccs.delCreditCard(id, new AsyncCallback<Void>() {
			public void onSuccess(Void result) {
				onLoad();
			}
			public void onFailure(Throwable caught) {}
		});
	}
	
	private CreditCard getObject(int row){
		CreditCard cc = new CreditCard();
		cc.setName(grid.getHTML(row, 1));
		cc.setItem(grid.getHTML(row, 2));
		String amount = grid.getHTML(row, 3);
		amount = amount.substring(0, amount.length()-1);
		cc.setAmount(Util.strToInt(amount));
		String value = grid.getHTML(row, 4);
		value = value.substring(2, value.length());
		value = value.substring(0, value.length()-1);
		cc.setBillDate(value);
		return cc;
	}
}
